package jucTest;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class SxResource {
    int i = 1;
    ReentrantLock lock = new ReentrantLock();
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();
    Condition c4 = lock.newCondition();

    public void get1() {
        lock.lock();
        try {
            while (i != 1) {
                c1.await();
            }
            System.out.println(Thread.currentThread().getName()+"==>"+"进入");
            i++;
            c2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get2() {
        lock.lock();
        try {
            while (i != 2) {
                c2.await();
            }
            System.out.println(Thread.currentThread().getName()+"==>"+"进入");
            i++;
            c3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get3() {
        lock.lock();
        try {
            while (i != 3) {
                c3.await();
            }
            System.out.println(Thread.currentThread().getName()+"==>"+"进入");
            i++;
            c4.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void get4() {
        lock.lock();
        try {
            while (i != 4) {
                c4.await();
            }
            System.out.println(Thread.currentThread().getName()+"==>"+"进入");
            i=1;
            c1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
